//
//  YXVistorView.swift
//  yanXiangWeiBo
//
//  Created by 颜祥 on 15/12/16.
//  Copyright © 2015年 yanxiang. All rights reserved.
//

import UIKit

// MARK: - 定义代理方法
protocol YXVistorViewDelegate: NSObjectProtocol
{
    // 点击注册
    func vistorViewRegister()
    
    // 点击登录
    func vistorViewLogin()
}


class YXVistorView: UIView
{
    // 设置代理属性
    weak var delegate: YXVistorViewDelegate?

    // MARK: -构造函数，创建访客视图
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        // 添加子控件
        setupUI()
        
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    // MARK: - 添加子控件
    private func setupUI()
    {
        backgroundColor = UIColor(white: 237/255.0, alpha: 1)
        // 添加子控件
        self.addSubview(iconView)
        self.addSubview(coverView)
        self.addSubview(houseView)
        self.addSubview(messageLabel)
        self.addSubview(registerBtn)
        self.addSubview(loginBtn)
        
        // 添加子控件的约束
        addSubViewConstraint()
       
    }
    
    
    // MARK: - 添加子控件的约束
    func addSubViewConstraint()
    {
         /************布局子控件************/
        // 转轮
        iconView.translatesAutoresizingMaskIntoConstraints = false
        
        // 添加约束
        addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        
        addConstraint(
            NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: -40))
        
        // 遮盖视图
        coverView.translatesAutoresizingMaskIntoConstraints = false
        
        addConstraint(NSLayoutConstraint(item: coverView, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Top, multiplier: 1, constant: 0))
        
        addConstraint(NSLayoutConstraint(item: coverView, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Width, multiplier: 1, constant: 0))
        
        addConstraint(NSLayoutConstraint(item: coverView, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: registerBtn, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 0))
        
        // 房子
        houseView.translatesAutoresizingMaskIntoConstraints = false
        
        // 添加约束
        addConstraint(NSLayoutConstraint(item: houseView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        
        addConstraint(NSLayoutConstraint(item: houseView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: 0))
        
        
        // 消息提示
        messageLabel.translatesAutoresizingMaskIntoConstraints = false
        
        // 添加约束
        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 18))
        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 224))
        
        
        
        // 注册按钮
        registerBtn.translatesAutoresizingMaskIntoConstraints = false
        
        // 添加约束
        addConstraint(NSLayoutConstraint(item: registerBtn, attribute: NSLayoutAttribute.Leading, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute: NSLayoutAttribute.Leading, multiplier: 1, constant: 0))
        
        addConstraint(NSLayoutConstraint(item: registerBtn, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 18))
        
        addConstraint(NSLayoutConstraint(item: registerBtn, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 100))
        
        addConstraint(NSLayoutConstraint(item: registerBtn, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 40))
        
        
        
        // 登录按钮
        loginBtn.translatesAutoresizingMaskIntoConstraints = false
        
        // 添加约束
        addConstraint(NSLayoutConstraint(item: loginBtn, attribute: NSLayoutAttribute.Trailing, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute: NSLayoutAttribute.Trailing, multiplier: 1, constant: 0))
        
        addConstraint(NSLayoutConstraint(item: loginBtn, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 18))
        
        addConstraint( NSLayoutConstraint(item: loginBtn, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 100))
        
        addConstraint(NSLayoutConstraint(item: loginBtn, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 40))

    }
    
    
    // MARK: - 不同的页面设置不同的信息
    func setupInfo(imageName: String, message: String)
    {
        // 设置图片
        iconView.image = UIImage(named: imageName)
        
        // 隐藏房子
        houseView.hidden = true
        
        // 设置消息提示
        messageLabel.text = message
        messageLabel.sizeToFit()
        
        // 将遮盖视图放到最下面
        sendSubviewToBack(coverView)
        
    }
    
    
    /***********以后碰到直接过来拷贝************/
    // MARK: - 旋转动画--首页显示
    func startRotationAnimation()
    {
        // 1.创建动画
        let animation = CABasicAnimation(keyPath: "transform.rotation")
        
        // 2.设置动画属性
        animation.toValue = M_PI * 2
        
        // 3.设置动画时间
        animation.duration = 20
        animation.repeatCount = MAXFLOAT
        
        // 4.动画停止时不移除
        animation.removedOnCompletion = false
        
        
        // 5.添加动画
        iconView.layer.addAnimation(animation, forKey: "homeRotation")
    }
    
    // 停止动画
    func pauseAnimation()
    {
        // 1.记录动画结束时间
        let pauseTime = iconView.layer.convertTime(CACurrentMediaTime(), fromLayer: nil)
        
        // 2.停止动画
        iconView.layer.speed = 0
        
        // 3.记录动画偏移时间
        iconView.layer.timeOffset = pauseTime
        
    }
    
    // 恢复动画
    func resumAnimation()
    {
        // 1.获取暂停时间
        let pauseTime = iconView.layer.timeOffset
        
        // 2.设置动画速度
        iconView.layer.speed = 1
        
        // 3.重置偏移时间
        iconView.layer.timeOffset = 0
        
        // 4.重置开始时间
        iconView.layer.beginTime = 0
        
        // 5.计算开始时间
        let sinceTime = iconView.layer.convertTime(CACurrentMediaTime(), fromLayer: nil) - pauseTime
        
        // 6.设置开始时间
        iconView.layer.beginTime = sinceTime
        
    }
    
    
    
    
    
    
    // MARK: - 懒加载
    // 转轮
    lazy var iconView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    
    // 遮盖视图
    lazy var coverView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    
    // 房子
    lazy var houseView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    
    // 消息提示
    private lazy var messageLabel : UILabel =
    {
        let label = UILabel()
        
        // 设置文字内容
        label.text = "关注一些人,看看这里有什么惊喜"
        
        // 设置字体颜色
        label.textColor = UIColor.orangeColor()
        
        
        // 设置分行显示
        label.numberOfLines = 0

        
        // 设置大小
        label.sizeToFit()
        
        
        
        return label
    }()
    
    // 注册按钮
    private lazy var registerBtn: UIButton =
    {
        let register = UIButton(type: UIButtonType.Custom)
        
        // 设置背景图片
        register.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        
        // 设置文字
        register.setTitle("注册", forState: UIControlState.Normal)
        
        // 设置文字颜色
        register.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal)
        
        // 添加点击事件
        register.addTarget(self, action: "registerBtnDidClick", forControlEvents: UIControlEvents.TouchUpInside)
        
        return register
        
    }()
    
    
    
    // 登录按钮
    private lazy var loginBtn: UIButton =
    {
        let login = UIButton(type: UIButtonType.Custom)
        
        // 设置背景图片
        login.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        
        // 设置文字
        login.setTitle("登录", forState: UIControlState.Normal)
        
        // 设置文字颜色
        login.setTitleColor(UIColor.blackColor(), forState: UIControlState.Normal)
        
        // 添加点击事件
        login.addTarget(self, action: "loginBtnDidClick", forControlEvents: UIControlEvents.TouchUpInside)
        
        return login
    }()
    
    // 点击注册
    @objc private func registerBtnDidClick()
    {
        //print(__FUNCTION__)
        delegate?.vistorViewRegister()
    }
    
    // 点击登录
    @objc private func loginBtnDidClick()
    {
        //print(__FUNCTION__)
        delegate?.vistorViewLogin()
    }
    
    

}
